/*

  The MIT License

  Copyright (c) 2008 - 2010. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
package
{

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

import ru.goodcore.core.GoodCore;
import ru.goodcore.utils.ActivityListener;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="400", height="300", frameRate="21", backgroundColor="#E0EEEE")]

public class DisplayObjectTreeTest extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function DisplayObjectTreeTest() {
		super();
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	protected override function creationComplete():void {
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var activityListener:ActivityListener = new ActivityListener();
		activityListener.width = 150;
		activityListener.x = 5;
		activityListener.y = 5;
		super.stage.addChild(activityListener);

		this.startTest();
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function startTest():void {
		super.stage.addEventListener(Event.ADDED, this.handler_added);
		super.stage.addEventListener(Event.REMOVED, this.handler_removed);
		super.stage.addEventListener(Event.REMOVED_FROM_STAGE, this.handler_removedFromStage);
		super.stage.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);

		var sprite_01:Sprite = new Sprite();
		sprite_01.name = 'sprite_01';
		sprite_01.addEventListener(Event.ADDED, this.handler_added);
		sprite_01.addEventListener(Event.REMOVED, this.handler_removed);
		sprite_01.addEventListener(Event.REMOVED_FROM_STAGE, this.handler_removedFromStage);
		sprite_01.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);

		var sprite_02:Sprite = new Sprite();
		sprite_02.name = 'sprite_02';
		sprite_02.addEventListener(Event.ADDED, this.handler_added);
		sprite_02.addEventListener(Event.REMOVED, this.handler_removed);
		sprite_02.addEventListener(Event.REMOVED_FROM_STAGE, this.handler_removedFromStage);
		sprite_02.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);

		var sprite_03:Sprite = new Sprite();
		sprite_03.name = 'sprite_03';
		sprite_03.addEventListener(Event.ADDED, this.handler_added);
		sprite_03.addEventListener(Event.REMOVED, this.handler_removed);
		sprite_03.addEventListener(Event.REMOVED_FROM_STAGE, this.handler_removedFromStage);
		sprite_03.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);

		var sprite_04:Sprite = new Sprite();
		sprite_04.name = 'sprite_04';
		sprite_04.addEventListener(Event.ADDED, this.handler_added);
		sprite_04.addEventListener(Event.REMOVED, this.handler_removed);
		sprite_04.addEventListener(Event.REMOVED_FROM_STAGE, this.handler_removedFromStage);
		sprite_04.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage);

		trace('\n\tsprite_01 ADD TO STAGE');
		super.stage.addChild(sprite_01);

		trace('\n\tsprite_02 ADD TO sprite_01');
		sprite_01.addChild(sprite_02);

		trace('\n\tsprite_03 ADD TO sprite_02');
		sprite_02.addChild(sprite_03);

		trace('\n\tsprite_04 ADD TO sprite_03');
		sprite_03.addChild(sprite_04);
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	private function handler_added(event:Event):void {
		trace('ADDED', event.currentTarget.name ? event.currentTarget.name : event.currentTarget, event.target.name, event.target.stage, event);

		if (event.target.name == 'sprite_03') {
			const parent:DisplayObjectContainer = event.target.parent as DisplayObjectContainer;
			//return;

			if (parent)
				parent.removeChild(event.target as DisplayObject);
			else 
				trace(event.target.name, ' has no parent');
		}
	}

	private function handler_removed(event:Event):void {
		trace('REMOVED', event.currentTarget.name ? event.currentTarget.name : event.currentTarget, event.target.name, event.target.stage, event);
	}

	private function handler_removedFromStage(event:Event):void {
		trace('REMOVED FROM STAGE', event.currentTarget.name ? event.currentTarget.name : event.currentTarget, event.target.name, event.target.stage, event);
	}

	private function handler_addedToStage(event:Event):void {
		trace('ADDED TO STAGE', event.currentTarget.name ? event.currentTarget.name : event.currentTarget, event.target.name, event.target.stage, event);
	}

	private function handler_addedToCradle(event:Event):void {
		event;
	}


}
}